package com.jsy.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.jsy.common.vo.Result;
import com.jsy.goods.feign.CategoryFeign;
import com.jsy.goods.feign.SpuFeign;
import com.jsy.goods.pojo.Category;
import com.jsy.goods.pojo.Goods;
import com.jsy.goods.pojo.Sku;
import com.jsy.goods.pojo.Spu;
import com.jsy.search.service.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageServiceImpl implements PageService {
    @Autowired
    private TemplateEngine templateEngine;

    @Autowired
    private CategoryFeign categoryFeign;

    @Autowired
    private SpuFeign spuFeign;

    //注入一个yml定义的文件路径
    @Value("${com.jsy.pagepath}")
    private String pagepath;

    /**
     * 根据商品spu生成静态页面
     *
     * @param id 商品spu id
     * @return
     * @throws FileNotFoundException
     */
    @Override
    public boolean createHtml(Long id) throws FileNotFoundException {
        //获取数据 spu skuList category imageList specMap
        Map<String, Object> dataMap = buildDataMap(id);
        Context context = new Context();
        context.setVariables(dataMap);
        //创建输出对象
        String filename = pagepath + id + ".html";
        File file = new File(filename);
        PrintWriter printWriter = new PrintWriter(file);
        templateEngine.process("item", context, printWriter);
        return true;
    }


    /**
     * 根据商品spu id删除静态页面
     *
     * @param id 商品spu id
     */
    @Override
    public void deleteHtml(Long id) {
        String filename = pagepath + id + ".html";
        File file = new File(filename);
        if (file.exists()) {
            file.delete();
        }
    }

    /**
     * 封装静态页面参数
     *
     * @param id
     * @return
     */
    private Map<String, Object> buildDataMap(Long id) {
        HashMap<String, Object> dataMap = new HashMap<>();
        Result<Goods> goodsResult = spuFeign.findGoodsBySpuId(id);
        if (goodsResult.getData() != null) {
            //spu
            Spu spu = goodsResult.getData().getSpu();
            dataMap.put("spu", spu);
            //skuList
            List<Sku> skuList = goodsResult.getData().getSkuList();
            dataMap.put("skuList", skuList);
            //imageList
            if (!StringUtils.isEmpty(spu.getImage())) {
                String images = spu.getImages();
                String[] imageList = images.split(",");
                dataMap.put("imageList", imageList);
            }
            //specMap
            Map specMap = JSON.parseObject(spu.getSpecItems(), Map.class);
            dataMap.put("specMap", specMap);
            //category
            Integer category1Id = spu.getCategory1Id();
            Result<Category> category1 = categoryFeign.findById(category1Id);
            dataMap.put("category1", category1.getData());
            Integer category2Id = spu.getCategory2Id();
            Result<Category> category2 = categoryFeign.findById(category2Id);
            dataMap.put("category2", category2.getData());
            Integer category3Id = spu.getCategory1Id();
            Result<Category> category3 = categoryFeign.findById(category3Id);
            dataMap.put("category3", category3.getData());
        }
        return dataMap;
    }
}
